package com.tianhui.consignortwofei.mvp.ui.activity.audit.vehicle;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.entity.PictureInfo;
import com.fgs.common.entity.region.RegionInfo;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.DateUtil;
import com.fgs.common.widget.itemView.CitySearchView;
import com.fgs.common.widget.itemView.ClickItemView;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.mvp.model.enty.VehicleInfo;
import com.tianhui.consignortwofei.mvp.model.enty.ocrResult.roadTransport.RoadTransportInfo;
import com.tianhui.consignortwofei.other.auth.PictureContext;
import com.tianhui.consignortwofei.other.auth.inte.SelectPhotoCallback;
import com.tianhui.consignortwofei.other.auth.stragety.VehicleRoadStrategy;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.picker.widget.TimePickerView;
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder;
import com.xuexiang.xui.widget.picker.widget.listener.OnTimeSelectListener;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class VehicleRoadAuditActivity extends BaseVehicleAuditActivity {

    @BindView(R.id.activity_vehicle_road_audit_plateNumberInputItemView)
    InputItemView mPlateNumberInputItemView;

    @BindView(R.id.activity_vehicle_road_audit_roadNumberInputItemView)
    InputItemView mRoadNumberInputItemView;
    @BindView(R.id.layout_deliver_source_path_citySearchView)
    CitySearchView mCitySearchView;

    @BindView(R.id.activity_vehicle_road_audit_businessNumberInputItemView)
    InputItemView mBusinessNumberInputItemView;

    @BindView(R.id.activity_vehicle_road_audit_modelInputItemView)
    InputItemView mModelInputItemView;


    @BindView(R.id.activity_vehicle_road_audit_issueDateClickItemView)
    ClickItemView mIssueDateClickItemView;

    @BindView(R.id.activity_vehicle_road_audit_endDateClickItemView)
    ClickItemView mEndDateClickItemView;

    private TimePickerView mIssueDatePickerView;
    private TimePickerView mEndDatePickerView;

    private String mRoadImageId;


    @Override
    public int getLayoutId() {
        return R.layout.activity_vehicle_road_audit;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @Override
    public void changePictureSuccess(PictureInfo pictureInfo) {

        mRoadImageId = pictureInfo.id;
    }

    @Override
    public String getToolbarSubtitle() {
        return "跳过";
    }

    @Override
    public void onSubtitleClick() {
        openActivityForResult(VehicleHeadAuditActivity.class, getIntent().getExtras());
    }


    @Override
    public void showVehicleInfo(VehicleInfo info) {
        mPlateNumberInputItemView.setContent(info.vehiclenum);
        mModelInputItemView.setContent(info.brandmodel);
        mRoadNumberInputItemView.setContent(info.roadtransportno);
        mBusinessNumberInputItemView.setContent(info.businessno);
        mIssueDateClickItemView.setContent(info.roadtransportbegintime);
        mEndDateClickItemView.setContent(info.roadtransportendtime);

        loadPicture(info.imgroadtransportimage);
        mRoadImageId = info.roadtransportimage;


    }

    @Override
    public VehicleInfo getSaveInfo() {
        String roadNumber = mRoadNumberInputItemView.getContent();
        String model = mModelInputItemView.getContent();
        String businessNumber = mBusinessNumberInputItemView.getContent();
        String issueDate = mIssueDateClickItemView.getContent();
        String endDate = mEndDateClickItemView.getContent();

        if (TextUtils.isEmpty(model)) {
            showToast("请输入品牌型号");
            return null;
        }
        if (TextUtils.isEmpty(roadNumber)) {
            showToast("请输入道路运输证号");
            return null;
        }
        if (TextUtils.isEmpty(businessNumber)) {
            showToast("请输入道路经营许可证号");
            return null;
        }
//        if (TextUtils.isEmpty(issueDate)) {
//            showToast("请选择发证日期");
//            return null;
//        }

        mVehicleInfo.brandmodel = model;
        mVehicleInfo.roadtransportno = roadNumber;
        mVehicleInfo.businessno = businessNumber;
        mVehicleInfo.roadtransportbegintime = issueDate;
        mVehicleInfo.roadtransportendtime = endDate;

        mVehicleInfo.roadtransportimage = mRoadImageId;

        return mVehicleInfo;
    }

    @Override
    public void nextAudit() {

        VehicleInfo vehicleInfo = getSaveInfo();
        if (vehicleInfo != null) {
            Bundle bundle = new Bundle();
            bundle.putParcelable(KeyValue.KEY_VEHICLE_INFO, vehicleInfo);
            bundle.putInt(KeyValue.KEY_OPERATE, getIntent().getExtras().getInt(KeyValue.KEY_OPERATE));
            openActivityForResult(VehicleHeadAuditActivity.class, bundle);
        }
    }

    //有效期
    @OnClick(R.id.activity_vehicle_road_audit_endDateClickItemView)
    public void selectEndDate() {
        hideKeyboard();
        if (mEndDatePickerView == null) {
            mEndDatePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String string = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    mEndDateClickItemView.setContent(string);
                }
            })
                    .setTitleText("请选择日期")
                    .build();
        }
        mEndDatePickerView.show();
    }

    //发证日期
    @OnClick(R.id.activity_vehicle_road_audit_issueDateClickItemView)
    public void selectIssueDate() {
        hideKeyboard();
        if (mIssueDatePickerView == null) {
            mIssueDatePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String string = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    mIssueDateClickItemView.setContent(string);
                }
            })
                    .setTitleText("请选择日期")
                    .build();
        }
        mIssueDatePickerView.show();
    }

    @OnClick(R.id.activity_vehicle_road_audit_changeModelTextView)
    public void changeModel() {
        new MaterialDialog.Builder(this)
                .content("确认要更改品牌型号吗？")
                .positiveText("是")
                .negativeText("否")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        mModelInputItemView.setItemEnable(true);
                    }
                })
                .show();
    }


    @OnClick(R.id.activity_vehicle_road_audit_businessNumberImageView)
    public void businessNumber() {
        mBusinessNumberInputItemView.setContent(mVehicleInfo.roadtransportno);
    }

    @OnClick(R.id.layout_audit_picture_editImageView)
    public void changePicture() {
        VehicleRoadStrategy vehicleRoadStrategy = (VehicleRoadStrategy) pictureStrategyFactory.createStrategy(VehicleRoadStrategy.class);
        pictureContext = new PictureContext(vehicleRoadStrategy);
        pictureContext.selectPhoto(getContext(), new SelectPhotoCallback<RoadTransportInfo>() {
            @Override
            public void selectPhotoSuccess(SelectPhotoInfo<RoadTransportInfo> resultInfo) {
                mRoadImageId = resultInfo.pictureId;
                loadPicture(resultInfo.realFileName);
                processVehicleRoadResult(resultInfo.resultInfo);
            }

            @Override
            public void selectPhotoFailed() {

            }
        });
    }

    private void processVehicleRoadResult(RoadTransportInfo resultInfo) {
        if (resultInfo == null) return;
        mRoadNumberInputItemView.setContent(resultInfo.license_number);
        mBusinessNumberInputItemView.setContent(resultInfo.business_certificate);

    }

    @Override
    public void init() {
        super.init();
        mCitySearchView.setOnCitySearchListener(new CitySearchView.OnCitySearchListener() {
            @Override
            public void citySearchCallback(RegionInfo regionInfo) {
                mCitySearchView.setContent(regionInfo.city);
                if (regionInfo.level.equals("2")) {
                    showVehicleRoadNumber(regionInfo.code);
                } else if (regionInfo.level.equals("3")) {
                    showVehicleRoadNumber(regionInfo.parentcode);
                } else {
                    showVehicleRoadNumber("");
                }
            }
        });
    }

    @OnClick(R.id.activity_vehicle_road_audit_createTextView)
    public void createVehicleRoadNumber() {
        String city = mCitySearchView.getContent();
        if (TextUtils.isEmpty(city)) {
            showToast("请输入城市名称");
            return;
        }
        requestCityInfo(city);
    }

    private void requestCityInfo(String city) {
        Map<String, String> map = new HashMap<>();
        map.put("city", city);
        mVehicleModel.getCityInfoModel(getContext(), map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {

                RegionInfo regionInfo = JsonUtil.fromJson(JsonUtil.toJson(commonResponse.getData()), RegionInfo.class);
                if (regionInfo != null) {
                    showVehicleRoadNumber(regionInfo.code);
                } else {
                    showToast("地区输入错误，请重新输入");
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }

    private void showVehicleRoadNumber(String code) {
        String vehicleRoadNumber = getVehicleRoadNumber(code);
        mRoadNumberInputItemView.setContent(vehicleRoadNumber);
    }

    private String getVehicleRoadNumber(String code) {
        if (!TextUtils.isEmpty(code)) {
            return code + "000000";
        } else {
            return "";
        }

    }


}
